package com.qiwx.pigeon.fragment;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.activity.MainActivity;
import com.qiwx.pigeon.activity.home.CageRepairActivity;
import com.qiwx.pigeon.activity.home.EliminateActivity;
import com.qiwx.pigeon.activity.home.FactoryQueryActivity;
import com.qiwx.pigeon.activity.home.HomeHelper;
import com.qiwx.pigeon.activity.home.PigeonPairActivity;
import com.qiwx.pigeon.activity.query.WXDCageActivity;
import com.qiwx.pigeon.activity.stock.StockManagerActivity;
import com.qiwx.pigeon.activity.summary.SummaryHomeActivity;
import com.qiwx.pigeon.adapter.home.HomeRemindAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPFragment;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.vo.HomeRemindBean;
import com.qiwx.pigeon.data.vo.RemindBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.presenter.HomePresenter;
import com.qiwx.pigeon.viewinterface.IHomeView;
import com.qiwx.pigeon.widget.itemderection.CustomGridItemDecoration;
import com.qiwx.pigeon.widget.itemderection.GridDividerItemDecoration;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

import butterknife.BindView;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 14:00
 * 首页
 **/
public class HomeFragment extends BaseMVPFragment<IHomeView, HomePresenter> implements IHomeView {
    @BindView(R.id.txt_all_amount)
    TextView txtAllAmount;
    @BindView(R.id.txtWNumber)
    TextView txtWNumber;
    @BindView(R.id.txt_wh_amount)
    TextView txtWhAmount;
    @BindView(R.id.txt_all_cl_amount)
    TextView txtAllClAmount;
    @BindView(R.id.txt_all_kl_amount)
    TextView txtAllKlAmount;
    @BindView(R.id.txt_wh_cl_amount)
    TextView txtWhClAmount;
    @BindView(R.id.txt_wh_kl_amount)
    TextView txtWhKlAmount;
    @BindView(R.id.txt_nodata)
    TextView txtNodata;
    @BindView(R.id.lin_nodata)
    LinearLayout linNodata;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.rel_nodata_recycleview)
    RelativeLayout relNodataRecycleview;
    List<HomeRemindBean> data = new ArrayList<>();
    HomeRemindAdapter adapter;
    CageParam cageParam;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initAfterOnCreate() {
        super.initAfterOnCreate();
        cageParam = new CageParam();
        cageParam.setF_id(AppContext.getFarmId(getContext()));
        initView();
//        initSpinner();
//        initData();
    }

    @Override
    public HomePresenter createPresenter() {
        return new HomePresenter(new WeakReference(this));
    }

    private void initView() {
        adapter = new HomeRemindAdapter(R.layout.adapter_home_remind_list_item, data);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerview.setLayoutManager(gridLayoutManager);
//        int space = ScreenUtils.dip2px(getActivity(), 1);
        recyclerview.addItemDecoration(new GridDividerItemDecoration(getContext()));
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, pos) -> {

            goRemindActivityDeatil(data.get(pos));

        });
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).setWareHouseListener(new MainActivity.WareHouseListener() {
                @Override
                public void onWhSuccess(List<WareHouseBean> list) {

                }

                @Override
                public void onSelectWareHouse(WareHouseBean wareHouseBean) {
                    initData();

                }
            });
        }


    }

    public void goRemindActivityDeatil(HomeRemindBean bean) {
        if (bean.getCl() != null) {
            Intent go = new Intent(getActivity(), bean.getCl());
            startActivity(go);
        } else {
            switch (bean.getType()) {
                case HomeHelper.REMIND_GZTW:
                    goCageChange();
                    break;
                case HomeHelper.REMIND_CDTX:
                    goCheckEgg();
                    break;
                case HomeHelper.REMIND_CKTX:
                    goCheckBaby();
                    break;
            }
        }
    }

    //跳转提醒详情
    public void goRemindActivityDeatil(int type) {
        switch (type) {
            case 0:
                goPigeonPair();
                break;
            case 1:
                goCageChange();
                break;
            case 2:
                goCageRepair();
                break;
            case 3:
                goCheckEgg();
                break;
            case 4:
                goCheckBaby();
                break;
            case 5:
                goEliminate();
                break;
            case 6:
                goEmptyCage();
                break;
            case 7:
                goSummaryData();
                break;
            case 8:
                goStock();
                break;
        }
    }

    public void addMessage(HomeRemindBean bean, int amount) {
        bean.setMessageAmount(amount);
        data.add(bean);
    }


    @Override
    public void dbUpdate() {
//        initSpinner();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    //更新指定的提醒数
    private void updateRemindAmount(int type, int amount) {
        for (int i = 0; i < data.size(); i++) {
            if (type == data.get(i).getType()) {
                data.get(i).setMessageAmount(amount);
                break;
            }

        }
    }

    //查询提醒消息
    public void queryRemindMessage(RemindBean remindBean) {
        data.clear();
        List<HomeRemindBean> temp = HomeHelper.getRemindList();
        data.addAll(temp);
        //配对提醒
        int pairAmount = remindBean.getPair_amount();
        updateRemindAmount(HomeHelper.REMIND_CXPD, pairAmount);

        //空笼提醒删除 改成未下蛋
//        int emptyAmount = remindBean.getA_kl_amount();
        int changeCageAmount = remindBean.getChange_amount();
        updateRemindAmount(HomeHelper.REMIND_GZTW, changeCageAmount);
        //维修
        int repairAmount = remindBean.getRepair_amount();
        updateRemindAmount(HomeHelper.REMIND_DWXSX, repairAmount);

        //查蛋提醒
        int checkEggAmount = remindBean.getCd_amount();
        updateRemindAmount(HomeHelper.REMIND_CDTX, checkEggAmount);

        //出壳提醒
        int checkBabyAmount = remindBean.getCz_amount();
        updateRemindAmount(HomeHelper.REMIND_CKTX, checkBabyAmount);

        //淘汰提醒
        int eliminateAmount = remindBean.getEliminate_amount();
        updateRemindAmount(HomeHelper.REMIND_ZGTT, eliminateAmount);

        updateRemindAmount(HomeHelper.REMIND_ZGTT, eliminateAmount);

//        int emptyAmount = 0;
//        if (emptyAmount >= 0) {
//            addMessage(temp.get(6), emptyAmount);
//        }
//        addMessage(temp.get(7), 0);
//        addMessage(temp.get(8), 0);

        if (data.size() == 0) {
            txtNodata.setVisibility(View.VISIBLE);
            txtNodata.setText("无提醒信息");
            linNodata.setVisibility(View.VISIBLE);
            recyclerview.setVisibility(View.GONE);
        } else {
            txtNodata.setVisibility(View.GONE);
            linNodata.setVisibility(View.GONE);
            recyclerview.setVisibility(View.VISIBLE);
        }
        adapter.notifyDataSetChanged();
    }

    //去配对
    public void goPigeonPair() {
        Intent go = new Intent(getActivity(), PigeonPairActivity.class);
        // go.putExtra("wh_id", wh_id);
        startActivity(go);
    }

    //去未下蛋
    public void goEmptyCage() {
//        Intent go = new Intent(getActivity(), CageEmptyActivity.class);
//        // go.putExtra("wh_id", wh_id);
//        startActivity(go);
        goWXDCage();
    }

    //去数据统计表
    public void goSummaryData() {
        Intent go = new Intent(getActivity(), SummaryHomeActivity.class);
        // go.putExtra("wh_id", wh_id);
        startActivity(go);
    }


    //待调窝
    public void goCageChange() {
        // Intent go = new Intent(getActivity(), CageChangeActivity.class);
        // go.putExtra("wh_id", wh_id);
        //startActivity(go);
        Activity activity = getActivity();
        if (activity != null && activity instanceof MainActivity) {
            ((MainActivity) activity).goAdjustFragment();
        }
    }

    //鸽笼维修
    public void goCageRepair() {
        Intent go = new Intent(getActivity(), CageRepairActivity.class);
        // go.putExtra("wh_id", wh_id);
        startActivity(go);
    }

    // 查蛋
    public void goCheckEgg() {

        Activity activity = getActivity();
        if (activity != null && activity instanceof MainActivity) {
            ((MainActivity) activity).goCdFragment();
        }

//        Intent go = new Intent(getActivity(), CheckEggActivity.class);
//        // go.putExtra("wh_id", wh_id);
//        startActivity(go);

    }

    // 查仔
    public void goCheckBaby() {
        Activity activity = getActivity();
        if (activity != null && activity instanceof MainActivity) {
            ((MainActivity) activity).goCzFragment();
        }
//        Intent go = new Intent(getActivity(), CheckBabyActivity.class);
////        // go.putExtra("wh_id", wh_id);
//        startActivity(go);

    }

    // 淘汰
    public void goEliminate() {
        Intent go = new Intent(getActivity(), EliminateActivity.class);
//        // go.putExtra("wh_id", wh_id);
        startActivity(go);

    }

    // 鸽场查询
    public void goStock() {
        Intent go = new Intent(getActivity(), StockManagerActivity.class);
//        // go.putExtra("wh_id", wh_id);
        startActivity(go);


    }


    public void initData() {
        txtWNumber.setText(MainActivity.wh_number + "号仓");
        cageParam.setWh_id(MainActivity.wh_id);
        mPresenter.queryRemindMessage(cageParam);
//        queryFactory();
//        queryWh();
//        queryRemindMessage();
    }

    @Override
    public void onQueryRemindSuccess(RemindBean bean) {
        int all = bean.getA_zs_amount();
        int kl = bean.getA_kl_amount();
        int wh_all = bean.getC_zs_amount();
        int wh_kl = bean.getC_kl_amount();

        txtWhAmount.setText("总笼数：" + wh_all + "个");
        txtWhClAmount.setText("存栏：" + (wh_all - wh_kl) + "个");
        txtWhKlAmount.setText("空笼：" + wh_kl + "个");

        txtAllAmount.setText("总笼数：" + all + "个");
        txtAllClAmount.setText("存栏：" + (all - kl) + "个");
        txtAllKlAmount.setText("空笼：" + kl + "个");
        queryRemindMessage(bean);


    }

    @Override
    public void onFailure(String error) {

    }

    public void goWXDCage() {
        startActivity(new Intent(getActivity(), WXDCageActivity.class));
    }

}
